package com.stable.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.stable.model.WorkBookCommon;
import com.stable.service.EastHsgtcgService;
import com.stable.service.FundInterestService;
import com.stable.utils.FileUtil;
import com.stable.utils.TradeDateUtil;
import com.stable.vo.EastHsgtcg;
import com.stable.vo.http.JsonResult;
import com.stable.vo.spi.req.EsQueryPageReq;
import com.stable.vo.spi.req.FundInterest;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 关注的基金列表
 */
@RequestMapping("/fundInterest")
@RestController
@Log4j2
public class FundInterestController {

    @Autowired
    private FundInterestService fundInterestService;


    /**
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> stocklist(String code,
                                                String name,
                                                EsQueryPageReq queryPage) {
        log.info("code:{},name:{}", code, name);
        queryPage.setPageSize(200);
        if (StringUtils.isEmpty(queryPage.getSortName())) {
            queryPage.setSortName("code");
        }
        JsonResult r = new JsonResult();
        try {
            r.setResult(fundInterestService.queryList(code, name, queryPage));
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }


    /**
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(@RequestBody JSONObject requestJson) {
        String code = StringUtils.isNotBlank(requestJson.getString("code")) ? requestJson.getString("code"): "";
        if (StringUtils.isEmpty(code)) {
            Map<String, String> map = new HashMap<>();
            map.put("result", "提交失败");
            return map;
        }
        String name = StringUtils.isNotBlank(requestJson.getString("name")) ? requestJson.getString("name"): "";
        String type = StringUtils.isNotBlank(requestJson.getString("type")) ? requestJson.getString("type"): "";
        FundInterest fundInterest = new FundInterest(code, name, type);

        fundInterestService.editFundInterest(fundInterest.getCode(), fundInterest);
        Map<String, String> map = new HashMap<>();
        map.put("result", "提交成功");
        return map;
    }




    /**
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> del(@RequestBody JSONObject requestJson) {
        String code = StringUtils.isNotBlank(requestJson.getString("code")) ? requestJson.getString("code"): "";
        if (StringUtils.isEmpty(code)) {
            Map<String, String> map = new HashMap<>();
            map.put("result", "提交失败");
            return map;
        }
        fundInterestService.delFundInterest(code);
        Map<String, String> map = new HashMap<>();
        map.put("result", "提交成功");
        return map;
    }


    /**根据基金代码，获取股票列表
     * @return
     */
    @RequestMapping(value = "/fundHoldGpQuery", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> fundHoldGpQuery(EsQueryPageReq queryPage) {
        queryPage.setPageSize(200);
        if (StringUtils.isEmpty(queryPage.getSortName())) {
            queryPage.setSortName("name");
        }
        JsonResult r = new JsonResult();
        try {
            r.setResult(fundInterestService.fundHoldGpQuery());
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }
}
